/**
	TABLESEARCH
**/
layui.define(['jquery', 'form', 'laydate'], function (exports) {

    var MOD_NAME = 'tableSearch',
		$ = layui.jquery,
		form = layui.form,
		laydate = layui.laydate;

	var tableSearch = {
		"v" : '1.0.0'
    };
	
	//缓存
	tableSearch.cache = {'data': {}, 'xm': {}, 'pages': {limit: 50}}

	//渲染
	tableSearch.render = function(opt){
	
		//配置默认值
		var elem = $(opt.elem || '#table'),
			searchs = opt.searchs || [],
			searchBarData = opt.searchBarData || {};
			opt.done = opt.done || function(){};
			
		//主运行
		var main = function (){
			elem.next().find('thead').append('<tr id="search-tr"></tr>');
			var theadTh = elem.next().find('thead').find('th');

			//渲染搜索栏
			layui.each(searchBarData, function (i, d){
				var value;
				if(tableSearch.cache.data[d.field] == undefined){
					tableSearch.cache.data[d.field] = '';
				}
				value = tableSearch.cache.data[d.field];

				if(d.type == "select"){
					var xm = xmSelect.render({
						el: '#'+d.field,
						radio: true,  //开启单选框
						autoRow: true,
						data: d.data
					});
					xm.setValue([value]);

					tableSearch.cache.xm['xm_'+d.field] = xm;
				}else if(d.type == "date"){
					laydate.render({
						elem: '#'+d.field, //指定元素
						type: d.format,
						value: value,
						trigger: 'click',
						range: true
					});
				}
			});

			layui.each(theadTh, function(i, th){
				$('#search-tr').append('<th search-field="'+$(th).attr('data-field')+'" search-key="1-0-'+i+'" class="">' +
					'<div class="layui-table-cell laytable-cell-1-0-'+i+'" align="center"></div></th>');
				//遍历搜索项
				for(let j in searchs){
					var searchField = searchs[j].field,
						searchType = searchs[j].type || "input",
						searchData = searchs[j].data,
						searchFormat = searchs[j].format;

					if($(th).attr('data-field') == searchField){
						var value;
						if(tableSearch.cache.data[searchField] == undefined){
							tableSearch.cache.data[searchField] = '';
						}
						value = tableSearch.cache.data[searchField];

						var searchElem = $('#search-tr').find('th[search-field="'+searchField+'"]').children();
						if(searchType == "input"){
							$(searchElem).append('<input type="text" class="layui-input" autocomplete="off" name="s-'+i+'-f" ' +
								'id="'+searchField+'" value="'+value+'" placeholder="请输入'+$($(th).find("span")).html()+'">');
						}else if(searchType == "select"){
							$(searchElem).append('<div id="'+searchField+'" class="xm-select-demo"></div>');

							var xm = tableSearch.cache.xm['xm_'+searchField];
							xm = xmSelect.render({
								el: '#'+searchField,
								radio: true,  //开启单选框
								autoRow: true,
								data: searchData
							});
							xm.setValue([value]);
							tableSearch.cache.xm['xm_'+searchField] = xm;
						}else if(searchType == "date"){
							$(searchElem).append('<input type="text" class="layui-input" name="s-'+i+'-f" id="'+searchField+'" placeholder=" - "autoComplete="off">');

							laydate.render({
								elem: '#'+searchField, //指定元素
								type: searchFormat,
								value: value,
								trigger: 'click',
								range: true
							});
						}else if(searchType == "radius"){
							if(tableSearch.cache.data[searchField+'Min'] == undefined){
								tableSearch.cache.data[searchField+'Min'] = '';
							}
							var minValue = tableSearch.cache.data[searchField+'Min'];
							if(tableSearch.cache.data[searchField+'Max'] == undefined){
								tableSearch.cache.data[searchField+'Max'] = '';
							}
							var maxValue = tableSearch.cache.data[searchField+'Max'];

							$(searchElem).append('<input type="number" class="layui-input" style="width:42%; float: left" autoComplete="off" id="'+searchField+'Min" value="'+minValue+'" min="0" placeholder="最小值"/>' +
								'-<input type="number" class="layui-input" style="width:42%; float: right" autoComplete="off" id="'+searchField+'Max" value="'+maxValue+'" min="0" placeholder="最大值"/>');
						}
						break;
					}
				}
			});

			//表格渲染
			form.render();

			opt.done();
		};
		main();

		//函数返回
		var returnObj = {
			'config' : opt,
			'reload' : function(opt){
				main();
			},
		}
		return returnObj
	}

	//调整表格xm-select组件展示
	tableSearch.form = function (opt){
		if($('#search-tr').find('.xm-select-demo').length){
			var headerH = $(opt).next().find('.layui-table-header').find('table').height();
			$(opt).next().find('.layui-table-header').css("height", "400px");
			$(opt).next().find('.layui-table-header').css("background-color", "#fff");
			$(opt).next().find('.layui-table-header').css("border-bottom", "none");
			$(opt).next().find('.layui-table-body ').css("top", -(400-headerH));
			$(opt).next().find('.layui-table-page ').css("top", -(400-headerH));
		}
	}

	//更新数据
	tableSearch.getData = function (opt){
		if($.isEmptyObject(tableSearch.cache.data)){
			//初始化数据
			var initData = opt.initData;
			layui.each(initData, function (i, d){
				var xm = tableSearch.cache.xm['xm_'+d.field];
				if(xm == undefined){
					tableSearch.cache.data[d.field] = d.data;
				}else{
					if(xm.getValue().length > 0){
						tableSearch.cache.data[d.field] = xm.getValue()[0].value;
					}
				}
			});
		}else{
			//更新选中数据
			layui.each(tableSearch.cache.data, function (i, d){
				if($('#'+i).length > 0){
					tableSearch.cache.data[i] = $('#'+i).val();
				}
			});
			layui.each(tableSearch.cache.xm, function (i, x){
				var f = i.replace('xm_', '');
				if(tableSearch.cache.xm[i].getValue().length > 0){
					tableSearch.cache.data[f] = tableSearch.cache.xm[i].getValue()[0].value;
				}
			});
		}

		return tableSearch.cache.data;
	}

	//重置数据
	tableSearch.resetData = function (opt){
		//清空选中数据
		layui.each(tableSearch.cache.data, function (i, d){
			if($('#'+i).length > 0){
				$('#'+i).val('');
				tableSearch.cache.data[i] = $('#'+i).val();
			}
		});
		layui.each(tableSearch.cache.xm, function (i, x){
			var f = i.replace('xm_', '');
			tableSearch.cache.xm[i].setValue([tableSearch.cache.data[f]]);
		});

		//初始化数据
		var initData = opt.initData;
		layui.each(initData, function (i, d){
			tableSearch.cache.data[d.field] = d.data;
			tableSearch.cache.xm['xm_'+d.field].setValue([d.data]);
		});
	}

	//输出接口
	exports(MOD_NAME, tableSearch);
});    